/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.controller;

import aviso.entity.AdvertQuery;
import aviso.facade.AdvertQueryFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author avgusti
 */
@ManagedBean
@ViewScoped
public class AdvertQueryController implements Serializable {

    /** Creates a new instance of AdvertQueryController */
    public AdvertQueryController() {
    }
    @EJB
    private AdvertQueryFacade advertQueryFacade;
    private AdvertQuery advertQuery = new AdvertQuery();
    private List<AdvertQuery> advertQueryList = new ArrayList<AdvertQuery>();
    private transient boolean listchanged = true;
    @ManagedProperty(value = "#{frontController}")
    private FrontController fc;

    public FrontController getFc() {
        return fc;
    }

    public void setFc(FrontController fc) {
        this.fc = fc;
    }

    

    public List<AdvertQuery> getAdvertQueryList() {
        if (listchanged) {
            advertQueryList = advertQueryFacade.listByUser(fc.getUser());
            listchanged = false;
        }
        return advertQueryList;
    }

    public void setAdvertQueryList(List<AdvertQuery> advertQueryList) {
        this.advertQueryList = advertQueryList;
    }

    public AdvertQuery getAdvertQuery() {
        return advertQuery;
    }

    public void setAdvertQuery(AdvertQuery advertQuery) {
        this.advertQuery = advertQuery;
    }

    public void pushAdvertQuery(AdvertQuery advertQuery) {
        this.advertQuery = advertQuery;
    }

    public void advertQuerySave() {
        if (advertQuery.getUser() == null) {
            advertQuery.setUser(fc.getUser());
        }
        if (advertQuery.getId() != null) {
            advertQueryFacade.edit(advertQuery);
        } else {
            advertQuery.setAdded_Date(Calendar.getInstance());
            advertQueryFacade.create(advertQuery);
        }
        listchanged = true;
    }

    public void advertQueryClear() {
        advertQuery = new AdvertQuery();
    }

    public void advertQueryRemove(long id) {
        AdvertQuery tmp=advertQueryFacade.find(id);
        advertQueryFacade.remove(tmp);
        if(advertQuery.getId()!=null && id==advertQuery.getId())
            advertQuery = new AdvertQuery();
        listchanged = true;
        
    }
    
}
